package main;

import maths.Vector3d;

public class ClosedSystem extends Box {
	public ClosedSystem(double x, double y, double z) {
		super(x, y, z);
	}

	/** implements boundary conditions for hard walled box */ 
	public void boxBoundaries(Particles p,String mode){
		if(mode.equals("closed")){
		for(int i = 0;i<p.getNumberOfParticles();i++){
			
			/*x = 0 boundary */
			if(p.getPosition(i)[0] < 0){
				// reflect velocity
				Vector3d newVelocity = new Vector3d(p.getVelocity(i));
				newVelocity.setY( -1*newVelocity.getY());
				newVelocity.setZ( -1*newVelocity.getZ());
				p.setVelocity(newVelocity.toArray(), i);
				
				
				// adjust position for overshoot,  how to work out error here?
				Vector3d newPosition = new Vector3d(p.getPosition(i));
				double overShoot = newPosition.getX() - this.getBoxSizeX(); 
				newPosition.setX(this.getBoxSizeX()-overShoot);
				p.setPosition(newPosition.toArray(),i);
				
				//totalImpulse = totalImpulse + (2*particles[i].getMass()*particles[i].getVelocity().magnitude());
			}
		
			
			/*x = L boundary */
			if(p.getPosition(i)[0] > this.getBoxSizeX()){
				// reflect velocity
				Vector3d newVelocity = new Vector3d(p.getVelocity(i));
				newVelocity.setY( -1*newVelocity.getY());
				newVelocity.setZ( -1*newVelocity.getZ());
				p.setVelocity(newVelocity.toArray(),i);
				
				
				// adjust position for overshoot,  how to work out error here?
				Vector3d newPosition = new Vector3d(p.getPosition(i));
				double overShoot = newPosition.getX() - this.getBoxSizeX(); 
				newPosition.setX(this.getBoxSizeX()-overShoot);
				p.setPosition(newPosition.toArray(),i);
				
				
			}
			
			
			/*y = 0 boundary */
			if(p.getPosition(i)[1] < 0){
				// reflect velocity
				Vector3d newVelocity = new Vector3d(p.getVelocity(i));
				newVelocity.setX( -1*newVelocity.getX());
				newVelocity.setZ( -1*newVelocity.getZ());
				p.setVelocity(newVelocity.toArray(),i);
				
				
				// adjust position for overshoot,  how to work out error here?
				Vector3d newPosition = new Vector3d(p.getPosition(i));
				double overShoot = newPosition.getY() - this.getBoxSizeY(); 
				newPosition.setY(this.getBoxSizeY()-overShoot);
				p.setPosition(newPosition.toArray(),i);
				
				//totalImpulse = totalImpulse + (2*particles[i].getMass()*particles[i].getVelocity().magnitude());
			}
		
			
			/*y = L boundary */
			if(p.getPosition(i)[1] > this.getBoxSizeY()){
				// reflect velocity
				Vector3d newVelocity = new Vector3d(p.getVelocity(i));
				newVelocity.setX( -1*newVelocity.getX());
				newVelocity.setZ( -1*newVelocity.getZ());
				p.setVelocity(newVelocity.toArray(),i);
				
				
				// adjust position for overshoot,  how to work out error here?
				Vector3d newPosition = new Vector3d(p.getPosition(i));
				double overShoot = newPosition.getY() - this.getBoxSizeY(); 
				newPosition.setY(this.getBoxSizeY()-overShoot);
				p.setPosition(newPosition.toArray(),i);
				
				
			}
			
			/*z = 0 boundary */
			if(p.getPosition(i)[2] < 0){
				// reflect velocity
				Vector3d newVelocity = new Vector3d(p.getVelocity(i));
				newVelocity.setX( -1*newVelocity.getX());
				newVelocity.setY( -1*newVelocity.getY());
				p.setVelocity(newVelocity.toArray(),i);
				
				
				// adjust position for overshoot,  how to work out error here?
				Vector3d newPosition = new Vector3d(p.getPosition(i));
				double overShoot = newPosition.getZ() - this.getBoxSizeZ(); 
				newPosition.setZ(this.getBoxSizeZ()-overShoot);
				p.setPosition(newPosition.toArray(),i);
				
				//totalImpulse = totalImpulse + (2*particles[i].getMass()*particles[i].getVelocity().magnitude());
			}
		
			
			/*z = L boundary */
			if(p.getPosition(i)[2] > this.getBoxSizeZ()){
				// reflect velocity
				Vector3d newVelocity = new Vector3d(p.getVelocity(i));
				newVelocity.setX( -1*newVelocity.getX());
				newVelocity.setY( -1*newVelocity.getY());
				p.setVelocity(newVelocity.toArray(),i);
				
				
				// adjust position for overshoot,  how to work out error here?
				Vector3d newPosition = new Vector3d(p.getPosition(i));
				double overShoot = newPosition.getZ() - this.getBoxSizeZ(); 
				newPosition.setX(this.getBoxSizeZ()-overShoot);
				p.setPosition(newPosition.toArray(),i);
				
				
				}
			}
		}
	}
}
